package com.samphanie.admin.modules.sys.domain.vo;

import com.samphanie.common.execetion.ApiException;
import com.samphanie.mbg.model.sys.SysRole;
import com.samphanie.mbg.model.sys.SysUser;
import io.swagger.annotations.ApiModelProperty;
import lombok.*;

import java.io.Serializable;
import java.util.List;
import java.util.Optional;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * @author ZSY
 * @date 2021/11/9 13:32
 */
@Data
@With
@Builder
@AllArgsConstructor
@NoArgsConstructor
public class UserProfileVo implements Serializable {
    private static final long serialVersionUID = -4339523550451056879L;
    public static Function<SysUser, UserProfileVo> fromUserProfileVo = (user) -> UserProfileVo.builder()
            .id(user.getId())
            .username(user.getUsername())
            .nickname(user.getNickname())
            .avatar(user.getAvatar())
            // 用户角色信息
            .roles(Optional.ofNullable(
                            user.getRoles().stream()
                                    .map(SysRole::getRoleName)
                                    .collect(Collectors.toList()))
                    .orElseThrow(() -> new ApiException("用户角色信息异常"))
            )
            .build();

    @ApiModelProperty(value = "ID")
    private Long id;
    @ApiModelProperty(value = "账户")
    private String username;
    @ApiModelProperty(value = "昵称")
    private String nickname;
    @ApiModelProperty(value = "头像")
    private String avatar;
    @ApiModelProperty(value = "角色信息")
    private List<String> roles;

}
